/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.states;

import PacketHandling.MapDecoder;
import PacketHandling.PacketHandler;
import horizon.graphics.RenderingContext;
import horizon.gui.LoadingScreenGui;
import horizon.map.Chunk;
import java.util.ArrayList;

/**
 *
 * @author J_Laptop
 */
public class GameLoadingState extends State {
    
    LoadingScreenGui gui;
    int totalPacketNumber = 0;
    int numberRecieved = 0;
    long time = 0;
    long authTime = 0;
    long authWait = 500;
    
    public GameLoadingState(String s) {
        
        super(s);
        
        PacketHandler.getInstance().addWorldDataRequest();
        PacketHandler.getInstance().sendCurrentRequest();
        
    }
    
    @Override
    public void init() {
        
        gui = new LoadingScreenGui(this);
        
    }
    
    @Override
    public void render(RenderingContext r) {
        
        gui.render(r);
        
    }
   
    @Override
    public void update(long elapsedTime) {
       
        totalPacketNumber = PacketHandler.getInstance().getPacketNumber();
        numberRecieved = PacketHandler.getInstance().getPacketRecieved();
        
        gui.loading.setMax(totalPacketNumber);
        gui.loading.setCur(numberRecieved);
        
        gui.update(elapsedTime);
        
        if(numberRecieved >= totalPacketNumber){
            
            if(authTime > authWait)
                StateManager.getInstance().pushAndRemovePrevious(new GameState(MapDecoder.createMap(PacketHandler.getInstance().getAvailableMapChunks(), PacketHandler.getInstance().getAvailableEntities()) , "Game"));            
            else
                authTime += elapsedTime;
        }
        
    }
    
    @Override
    public void actionGui(String s) {
        
    }
}
